package com.example.xinli.controller;

import com.example.xinli.domain.vo.WxGetPhoneVo;
import com.example.xinli.service.IUserService;
import com.example.xinli.domain.vo.LoginVo;
import com.example.xinli.domain.vo.ResponseBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@Slf4j
public class LoginController {

    @Autowired
    private IUserService userService;

    // 微信登录
    @RequestMapping("/loginByWechat")
    @ResponseBody
    public ResponseBean loginByWechat(@RequestBody LoginVo loginVo) {
        return userService.doLogin(loginVo);
    }

    @RequestMapping("/getphone")
    @ResponseBody
    public ResponseBean getPhoneNumber(@RequestBody WxGetPhoneVo phoneVo) {
        return userService.getPhoneNumber(phoneVo);
    }

    // 退出登录
    @RequestMapping("/logout")
    @ResponseBody
    public ResponseBean logout(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        token = token.split(" ")[1];
        return userService.doLogout(token);
    }

    @RequestMapping("/getAccessToken")
    @ResponseBody
    public ResponseBean getAccessToken() {
        return userService.getAccessToken();
    }
}

